package com.zm.sgin;

import com.zm.sgin.bean.SginProperties;
import com.zm.sgin.web.LoginInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.sql.DataSource;


@Configuration
@ComponentScan("com.zm.sgin")
@Slf4j
@ConditionalOnProperty(prefix = "sgin",name = "enable",havingValue = "true")
public class SginConfig implements WebMvcConfigurer {
    // 任何访问数据库： MyBatis or Jdbc

    @Bean
    @ConditionalOnBean(JdbcTemplate.class)
    JdbcTemplate jdbcTemplate(DataSource dataSource){
        return new JdbcTemplate(dataSource);
    }
    @Autowired
    SginProperties properties;
    @Autowired
    LoginInterceptor loginInterceptor;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns(properties.getUrls());
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/sgin/**")
                .addResourceLocations("file:F:\\yuancheng\\javaproject\\s3\\git\\phase-iii-homework-warehouse\\springboot\\starter-sgin\\src\\main\\resources\\static/");
    }
}
